package com.weilai.service.user;

import com.weilai.bean.model.SysUser;
import com.weilai.service.SysUser.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录校验方法
 * 
 * @author ruoyi
 */
@Component
public class LoginService {
    @Autowired
    private PasswordService passwordService;

    @Autowired
    private IUserService userService;

    /**
     * 登录
     */
    public SysUser login(String username, String password)
    {
        // 验证码校验
//        if (!StringUtils.isEmpty(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA)))
//        {
//            SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("SysUser.jcaptcha.error"));
//            throw new CaptchaException();
//        }
        // 用户名或密码为空 错误
//        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
//        {
//            SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null"));
//            throw new UserNotExistsException();
//        }
        // 密码如果不在指定范围内 错误
//        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
//                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
//        {
//            SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("SysUser.password.not.match"));
//            throw new UserPasswordNotMatchException();
//        }
//
//        // 用户名不在指定范围内 错误
//        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
//                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
//        {
//            SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("SysUser.password.not.match"));
//            throw new UserPasswordNotMatchException();
//        }
//
        // 查询用户信息
        SysUser SysUser = userService.selectUserByLoginName(username);
//
//        if (SysUser == null && maybeMobilePhoneNumber(username))
//        {
//            SysUser = userService.selectUserByPhoneNumber(username);
//        }
//
//        if (SysUser == null && maybeEmail(username))
//        {
//            SysUser = userService.selectUserByEmail(username);
//        }
//
//        if (SysUser == null || UserStatus.DELETED.getCode().equals(SysUser.getDelFlag()))
//        {
//            SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("SysUser.not.exists"));
//            throw new UserNotExistsException();
//        }

//        passwordService.validate(SysUser, password);

//        if (UserStatus.DISABLE.getCode().equals(SysUser.getStatus()))
//        {
//            SystemLogUtils.log(username, Constants.LOGIN_FAIL, MessageUtils.message("SysUser.blocked", SysUser.getRemark()));
//            throw new UserBlockedException(SysUser.getRemark());
//        }
//        SystemLogUtils.log(username, Constants.LOGIN_SUCCESS, MessageUtils.message("SysUser.login.success"));
//        recordLoginInfo(SysUser);
        return SysUser;
    }

//    private boolean maybeEmail(String username)
//    {
//        if (!username.matches(UserConstants.EMAIL_PATTERN))
//        {
//            return false;
//        }
//        return true;
//    }

//    private boolean maybeMobilePhoneNumber(String username)
//    {
//        if (!username.matches(UserConstants.MOBILE_PHONE_NUMBER_PATTERN))
//        {
//            return false;
//        }
//        return true;
//    }

    /**
     * 记录登录信息
     */
//    public void recordLoginInfo(SysUser SysUser)
//    {
//        SysUser.setLoginIp(ShiroUtils.getIp());
//        SysUser.setLoginDate(DateUtils.getNowDate());
//        userService.updateUserInfo(SysUser);
//    }

}
